//
//  WDThinkingManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/13.
//

#import "WDThinkingManager.h"
#import "ThinkingSDK/ThinkingSDK.h"
@implementation WDThinkingManager
{
    ThinkingAnalyticsSDK *instance;
}
static WDThinkingManager *_ThinkingManager = nil;
+ (instancetype)shareThinkingManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _ThinkingManager = [[self alloc] init];
//        [_ThinkingManager thinkingConfig];
    });
    return _ThinkingManager;
}

- (void)thinkingConfig
{
    if (instance)
    {
        return;
    }


    
//    [[ThinkingAnalyticsSDK sharedInstance] enableThirdPartySharing:TDThirdPartyShareTypeIRONSOURCE];
}

- (void)thinkingTrackEventWithKey:(NSString *)key jsonStr:(id)jsonStr
{
    NSDictionary *dict;
    if ([jsonStr isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)jsonStr;
    }
    else if ([jsonStr isKindOfClass:[NSString class]]){
        NSDictionary *temp = [self dictionaryWithJsonString:(NSString *)jsonStr];
        if (temp)
        {
            dict = temp;
        }
    }
    NSLog(@"thinkingTrackEventWithKey:%@",dict);
    if (!instance) {
        [self thinkingConfig];
    }
    [instance track:key properties:dict];
}
#pragma makr----字符串转字典
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(dic)
    {
        return dic;
    }else{
        return nil;
    }
}


- (void)thinkingTrackLevelEnter:(NSInteger)level
{
    
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_enter" jsonStr:dict];
}

- (void)thinkingTrackLevelStart:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_start" jsonStr:dict];
}

- (void)thinkingTrackLevelWin:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_win" jsonStr:dict];
}

- (void)thinkingTrackLevelFail:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_fail" jsonStr:dict];
}

- (void)thinkingTrackLevelRevive:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_revive" jsonStr:dict];
}

- (void)thinkingTrackLevelRetry:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_retry" jsonStr:dict];
}

- (void)thinkingTrackLevelProp:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_prop" jsonStr:dict];
}

- (void)thinkingAdd:(id)json
{
    NSDictionary *dict;
    if ([json isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)json;
    }
    else if ([json isKindOfClass:[NSString class]]){
        NSDictionary *temp = [self dictionaryWithJsonString:(NSString *)json];
        if (temp)
        {
            dict = temp;
        }
    }
    if (!instance) {
        [self thinkingConfig];
    }
    [instance user_add:dict];
}

- (void)thinkingSuperProperties:(id)json
{
    NSDictionary *dict;
    if ([json isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)json;
    }
    else if ([json isKindOfClass:[NSString class]]){
        NSDictionary *temp = [self dictionaryWithJsonString:(NSString *)json];
        if (temp)
        {
            dict = temp;
        }
    }
    if (!instance) {
        [self thinkingConfig];
    }
    [instance setSuperProperties:dict];//设置公共事件属性
}

- (void)thinkingTrackUserProperty:(id)json
{
    NSDictionary *dict;
    if ([json isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)json;
    }
    else if ([json isKindOfClass:[NSString class]]){
        NSDictionary *temp = [self dictionaryWithJsonString:(NSString *)json];
        if (temp)
        {
            dict = temp;
        }
    }
    if (!instance) {
        [self thinkingConfig];
    }
    [instance user_set:dict];
}

- (void)thinkingUser_setOnce:(id)json
{
    NSDictionary *dict;
    if ([json isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)json;
    }
    else if ([json isKindOfClass:[NSString class]]){
        NSDictionary *temp = [self dictionaryWithJsonString:(NSString *)json];
        if (temp)
        {
            dict = temp;
        }
    }
    if (!instance) {
        [self thinkingConfig];
    }
    [instance user_setOnce:dict];
}

- (void)thinkingUser_setOnceWithKey:(NSString *)key value:(NSString *)value
{
    if (!instance) {
        [self thinkingConfig];
    }
    [instance user_setOnce:@{key:value}];
}

@end
